package vn.codespot.hms.model.request;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import vn.codespot.hms.model.request.ApplyRequestModel.Applicant;

public class ConfirmRequestModel extends AbsRequestModel {
	public static String vlDate_Arrival = "";

    public static final String DATE_ARRIVAL = "date_arrival";

    public static final String HOUR_ARRIVAL = "hour_arrival";

    public static final String DATE_EXIT = "date_exit";

    public static final String PERSON = "person";

//    public static final String URGENTLY = "urgently";

    public static final String APPLICANTS = "applicants";

    public static final String PRIMARY_EMAIL = "primary_email";

    public static final String SECONDARY_EMAIL = "secondary_email";

    public static final String NOTE = "note";

    public static final String VISA_TYPE = "visa_type";

    public static final String PURPOSE = "purpose";

    public static final String ARRIVAL_PORT = "arrival_port";

//    public static final String FEES = "fees";

//    public static final String COUPON_CODE = "coupon_code";

    private Map<String, String> mData;

    private List<Map<String, String>> mApplicants;

    public ConfirmRequestModel() {
        mData = new HashMap<String, String>();
        mApplicants = new ArrayList<Map<String, String>>();
    }

    public void setVisaType(String visaType) {
    	if (visaType != null && !visaType.isEmpty()) {
    		mData.put(VISA_TYPE, visaType);
		}
    }

    public void setPurpose(String purpose) {
    	if (purpose != null && !purpose.isEmpty()) {
    		mData.put(PURPOSE, purpose);
		}
        
    }

    public void setArrivalPort(String arrivalPort) {
    	if (arrivalPort != null && !arrivalPort.isEmpty()) {
    		mData.put(ARRIVAL_PORT, arrivalPort);
		}
        
    }

    public void setDateArrival(String dateArrival) {
        if (dateArrival.contains("/")) {
            dateArrival = dateArrival.replace('/', '-');
        }
        vlDate_Arrival = dateArrival;
        if (dateArrival != null && !dateArrival.isEmpty()) {
        	mData.put(DATE_ARRIVAL, dateArrival);
		}
        
    }

    public void setHourArrival(String hourArrival) {
    	if (hourArrival != null && !hourArrival.isEmpty()) {
    		mData.put(HOUR_ARRIVAL, hourArrival);
		}
        
    }

    public void setDateExit(String dateExit) {
        if (dateExit.contains("/")) {
            dateExit = dateExit.replace('/', '-');
        }
        if (dateExit != null && !dateExit.isEmpty()) {
        	mData.put(DATE_EXIT, dateExit);
		}
        
    }

    public void setPrimaryEmail(String email) {
    	if (email != null && !email.isEmpty()) {
    		mData.put(PRIMARY_EMAIL, email);
		}
        
    }

    public void setSecondaryEmail(String email) {
    	if (email != null && !email.isEmpty()) {
    		mData.put(SECONDARY_EMAIL, email);
		}
    }

    public void setNote(String note) {
    	if (note != null && !note.isEmpty()) {
    		mData.put(NOTE, note);
		}
        
    }

//    public void setFees(String fees) {
//        try {
//            Integer.valueOf(fees);
//            mData.put(FEES, fees);
//        } catch (NumberFormatException e) {
//            LogUtil.e("Cannot set fee", e);
//            mData.put(FEES, fees.substring(0, fees.length() - 1));
//        }
//    }

//    public void setCouponCode(String couponCode) {
//        mData.put(COUPON_CODE, couponCode);
//    }

//    public void setUrgently(boolean urgently) {
//        Date date = null;
//        Date currDate = null;
//        Date now = new Date();
//        String nowDate = "";
//        DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
//        try {
//            date = formatter.parse(vlDate_Arrival);
//            nowDate = formatter.format(now);
//            currDate = formatter.parse(nowDate);
//        } catch (ParseException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
//
//        if (urgently) {
//            mData.put(URGENTLY, "1");
//        } else if (date.compareTo(currDate) == 0) {
//            mData.put(URGENTLY, "1");
//        }
//    }

    public void addApplicant(Applicant applicant) {
        mApplicants.add(applicant.getData());
        mData.put(PERSON, String.valueOf(mApplicants.size()));
    }

    public void setApplicants(Collection<Applicant> applicants) {
        for (Applicant applicant : applicants) {
            addApplicant(applicant);
        }
    }

//    public static class Applicant {
//        public static final String PASSPORT_EXP = "passport_exp";
//
//        public static final String PASSPORT = "passport";
//
//        public static final String NATIONAL = "national";
//
//        public static final String BIRTHDAY = "birthday";
//
//        public static final String GENDER = "gender";
//
//        public static final String NAME = "name";
//
//        public Map<String, String> mData;
//
//        public Applicant() {
//            mData = new HashMap<String, String>();
//        }
//
//        public void setName(String name) {
//            mData.put(NAME, name);
//        }
//
//        public void setGender(String gender) {
//            mData.put(GENDER, gender);
//        }
//
//        public void setBirthday(String birthday) {
//            if (birthday.contains("/")) {
//                birthday = birthday.replace('/', '-');
//            }
//            mData.put(BIRTHDAY, birthday);
//        }
//
//        public void setNational(String national) {
//            mData.put(NATIONAL, national);
//        }
//
//        public void setPassport(String passport) {
//            mData.put(PASSPORT, passport);
//        }
//
//        public void setPassportExp(String passportExp) {
//            if (passportExp.contains("/")) {
//                passportExp = passportExp.replace('/', '-');
//            }
//            mData.put(PASSPORT_EXP, passportExp);
//        }
//
//        public Map<String, String> getData() {
//            return mData;
//        }
//
//        public String getValue(String name) {
//            return mData.get(name);
//        }
//
//        public String getGender() {
//            return mData.get(GENDER);
//        }
//
//        public String getNational() {
//            return mData.get(NATIONAL);
//        }
//
//        public String getName() {
//            return mData.get(NAME);
//        }
//
//        public String getPassport() {
//            return mData.get(PASSPORT);
//        }
//    }

    @Override
    public String convertToJson() {
        /*JSONObject jsonObj = JSonUtils.getJSonObject(mData);
        JSONArray jsonArr = JSonUtils.getJsonArray(mApplicants);
        JSonUtils.addJSonArary(jsonObj, APPLICANTS, jsonArr);
        return JSonUtils.getJSonAsString(jsonObj);*/

        StringBuilder builder = new StringBuilder();
        for (Entry<String, String> data : mData.entrySet()) {
            builder.append(data.getKey());
            builder.append("=");
            builder.append(data.getValue());
            builder.append("&");
        }
        String format = APPLICANTS + "[%s][%s]=%s";
        for (int i = 0; i < mApplicants.size(); i++) {
            for (Entry<String, String> applicantData : mApplicants.get(i).entrySet()) {
                builder.append(String.format(format, i, applicantData.getKey(), applicantData.getValue()));
                builder.append("&");
            }
        }
        String ret = builder.toString();
        if (ret.endsWith("&")) {
            ret = ret.substring(0, ret.length() - 1);
        }
        return ret;
    }

    @Override
    protected Map<String, String> getData() {
        return null;
    }
}
